﻿
/*----------------------------------------------------------------
 * 创建时间：2023/8/21 14:12:52
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Modules.Business.Service.Resources 
 * 描  述：资源管理逻辑接口-实现
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using Minotaur.App.Core;
using Minotaur.App.Core.Attributes;
using Minotaur.App.Core.Exceptions;
using Minotaur.App.Domains.Model.Resouces;
using Minotaur.App.Domains.Model.Table;
using Minotaur.App.Domains.Request;
using Minotaur.App.Domains.ViewModel.Resources;
using Minotaur.App.Domains.ViewModel.Table;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minotaur.App.Modules.Business.Service.Resources
{
    /// <summary>
    /// 资源管理逻辑接口-实现
    /// </summary>
    public class ResourcesService : IResourcesService
    {
        private readonly IDbContext dbContext;
        private readonly GlobalUserModel userModel;

        public ResourcesService(IDbContext dbContext, GlobalUserModel userModel)
        {
            this.dbContext = dbContext;
            this.userModel = userModel;
        }
        [GlobalLogAspect]
        public async Task<ResourcesRespone> InitResources(ResourcesRequest request)
        {
            ResourcesRespone respone = new ResourcesRespone();
            RefAsync<int> total = 0;
            try
            { 
                int lan = (int)((userModel.Language + "").ConvertEnumEvent<Domains.Enum.LanguageType>());
                var queryable = this.dbContext.BusinessDb.Queryable<ResoucesInfo>()
                    .LeftJoin<ResoucesInfoRes>((a, b) => a.Autoid == b.ResourceId && b.Lan == lan)
                    .Where((a) => !a.IsDelete && a.MenuCode == request.MenuCode);
                if (!string.IsNullOrEmpty(request.SearchQuery))
                {
                    queryable.Where((a, b) => a.ResCode.Contains(request.SearchQuery) || b.Value.Contains(request.SearchQuery) || a.MenuCode.Contains(request.SearchQuery));
                }
                List<ResourcesViewModel> TableInfoList = await queryable
                       .Select((a, b) => new ResourcesViewModel()
                       {
                           Autoid = a.Autoid,
                           MenuCode = a.MenuCode,
                           ResCode = a.ResCode,
                           Resoucestype = a.Resoucestype,
                           ResName = b.Value,
                           IsEnable = a.IsEnable,
                           Creator = a.Creator.ToString(),
                           CreatDate = a.CreatDate.HasValue ? a.CreatDate.Value.ToString("yyyy-MM-dd HH:mm") : "",
                       }).ToOffsetPageAsync(request.PageIndex, request.PageSize, total);
                respone.TotalCount = total.Value;
                respone.List = TableInfoList;
            }
            catch (Exception ex)
            {
                throw new GlobalException(typeof(ResourcesService), System.Reflection.MethodBase.GetCurrentMethod().Name, request, this.userModel, ex.Message);
            }
            return respone;
        }
    }
}
